package org.tcx.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.util.DOMUtil;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class DomUtil {
	public static Element createElementNS(String nameSpcace, String elmentName)
			throws ParserConfigurationException {
		Document doc = newDocument();
		Element ele = doc.createElementNS(nameSpcace, elmentName);
		return ele;
	}
	public static Document newDocument() throws ParserConfigurationException {
		DocumentBuilderFactory builderFactory =
				DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = builderFactory.newDocumentBuilder();
		Document doc = builder.newDocument();
		return doc;
	}

	public static Element createElement(String elmentName) throws ParserConfigurationException {
		Document doc = newDocument();
		Element ele = doc.createElement(elmentName);
		return ele;
	}


	public static Element createChildElement(Element parent,
			String childElmentName) {
		Document doc = parent.getOwnerDocument();
		Element childEle = doc.createElement(childElmentName);
		parent.appendChild(childEle);
		return childEle;
	}

	public static Text createChildText(Element parent, String child) {
		Text childText = null;
		if (child != null) {
			Document doc = parent.getOwnerDocument();
			childText = doc.createTextNode(child);
			parent.appendChild(childText);
		}
		return childText;
	}

	public static void writeToFile(Document doc, File file)
			throws TransformerException {
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(file);
		transformer.transform(source, result);
	}

	public static void writeToOutputStream(Document doc, OutputStream out)
			throws TransformerException {
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(out);
		transformer.transform(source, result);
	}

	public static void writeToFileFormat(Document doc, File file)
			throws IOException {
		OutputFormat format = new OutputFormat(doc, "UTF-8", true);
		FileOutputStream stream = null;
		try {
			stream = new FileOutputStream(file);
			XMLSerializer serial = new XMLSerializer(stream, format);
			serial.serialize(doc);
		} finally {
			IOUtil.silentlyClose(stream);
		}
	}

	public static void writeToFile(Node node, File file)
			throws TransformerException {
		Document doc = DOMUtil.getDocument(node);
		if (DOMUtil.getRoot(doc) == null) {
			doc.appendChild(node);
		}
		writeToFile(doc, file);
	}

	public static void writeToFileFormat(Node node, File file)
			throws IOException {
		Document doc = DOMUtil.getDocument(node);
		if (DOMUtil.getRoot(doc) == null) {
			doc.appendChild(node);
		}
		writeToFileFormat(doc, file);
	}

	public static void writeToOutputStream(Node node, OutputStream out)
			throws TransformerException {
		Document doc = DOMUtil.getDocument(node);
		if (DOMUtil.getRoot(doc) == null) {
			doc.appendChild(node);
		}
		writeToOutputStream(doc, out);
	}

	public static Document parse(InputStream stream)
			throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;
		DocumentBuilder builder = factory.newDocumentBuilder();
		doc = builder.parse(stream);
		doc.normalize();
		return doc;
	}
}
